clear all
set more off
cd /Users/zimaoxiao/Dropbox/CC_Yield_Predict/replication_package/  // Navigate to replication folder on your own machine
global weather "timeInt0_3 timeInt3_6 timeInt6_9 timeInt9_12 timeInt12_15 timeInt15_18 timeInt18_21 timeInt21_24 timeInt24_27 timeInt27_30 timeInt30_33 timeInt33_36 timeInt36_39 prec prec2"

cap mkdir data/figure

qui {	
* 0
* run all the regressions to retrive coefficients
use if inrange(year,1950,2000) using data/regression/reg_2020_prediction_3Cstep, clear
merge m:1 county_fips using data/yield/filter, keep(3) nogenerate
reghdfe lncornyield $weather state_fips#c.trend [aweight=corn_areaHarv], absorb(county_fips) vce(cluster county_fips StateXYear)
tempfile corn_st_lin_2020
parmest, label saving(`"`corn_st_lin_2020'"', replace) format(p %8.2f) stars(0.1 0.05 0.01)
	
reghdfe lncornyield $weather state_fips#c.trend state_fips#c.trend2 [aweight=corn_areaHarv], absorb(county_fips) vce(cluster county_fips StateXYear)
tempfile corn_st_quad_2020
parmest, label saving(`"`corn_st_quad_2020'"', replace) format(p %8.2f) stars(0.1 0.05 0.01)
	
reghdfe lncornyield $weather [aweight=corn_areaHarv], absorb(county_fips state_fips#year) vce(cluster county_fips StateXYear)
tempfile corn_st_fe_2020
parmest, label saving(`"`corn_st_fe_2020'"', replace) format(p %8.2f) stars(0.1 0.05 0.01)
	
use if inrange(year,1950,2020) using data/regression/reg_2050_prediction_3Cstep, clear
merge m:1 county_fips using data/yield/filter, keep(3) nogenerate
reghdfe lncornyield $weather state_fips#c.trend [aweight=corn_areaHarv], absorb(county_fips) vce(cluster county_fips StateXYear)
tempfile corn_st_lin_2050
parmest, label saving(`"`corn_st_lin_2050'"', replace) format(p %8.2f) stars(0.1 0.05 0.01)
	
reghdfe lncornyield $weather state_fips#c.trend state_fips#c.trend2 [aweight=corn_areaHarv], absorb(county_fips) vce(cluster county_fips StateXYear)
tempfile corn_st_quad_2050
parmest, label saving(`"`corn_st_quad_2050'"', replace) format(p %8.2f) stars(0.1 0.05 0.01)
	
reghdfe lncornyield $weather [aweight=corn_areaHarv], absorb(county_fips state_fips#year) vce(cluster county_fips StateXYear)
tempfile corn_st_fe_2050
parmest, label saving(`"`corn_st_fe_2050'"', replace) format(p %8.2f) stars(0.1 0.05 0.01)

* 1
* state linear trends, 2020
use parm label estimate stderr min95 max95 using `corn_st_lin_2020', clear
drop if _n>13
replace label = "State linear trends"
gen layer=1
gen xcoord=0
forvalues i = 1/13 {
	local x=`i'-1
	replace xcoord=1.5+3*`x' if _n==`i'
}
tempfile stlin2020
save `stlin2020', replace

* 2
* state quadratic trends, 2020
use parm label estimate stderr min95 max95 using `corn_st_quad_2020', clear
drop if _n>13
replace label = "State quadratic trends"
gen layer=1
gen xcoord=0
forvalues i = 1/13 {
	local x=`i'-1
	replace xcoord=1.5+3*`x' if _n==`i'
}
tempfile stquad2020
save `stquad2020', replace

* 3
* state-by-year fe, 2020
use parm label estimate stderr min95 max95 using `corn_st_fe_2020', clear
drop if _n>13
replace label = "State-by-year FE"
gen layer=1
gen xcoord=0
forvalues i = 1/13 {
	local x=`i'-1
	replace xcoord=1.5+3*`x' if _n==`i'
}
tempfile stfe2020
save `stfe2020', replace

* 4
* state linear trends, 2020
use parm label estimate stderr min95 max95 using `corn_st_lin_2050', clear
drop if _n>13
replace label = "State linear trends"
gen layer=2
gen xcoord=0
forvalues i = 1/13 {
	local x=`i'-1
	replace xcoord=1.5+3*`x' if _n==`i'
}
tempfile stlin2050
save `stlin2050', replace

* 5
* state quadratic trends, 2020
use parm label estimate stderr min95 max95 using `corn_st_quad_2050', clear
drop if _n>13
replace label = "State quadratic trends"
gen layer=2
gen xcoord=0
forvalues i = 1/13 {
	local x=`i'-1
	replace xcoord=1.5+3*`x' if _n==`i'
}
tempfile stquad2050
save `stquad2050', replace

* 6
* state-by-year fe, 2020
use parm label estimate stderr min95 max95 using `corn_st_fe_2050', clear
drop if _n>13
replace label = "State-by-year FE"
gen layer=2
gen xcoord=0
forvalues i = 1/13 {
	local x=`i'-1
	replace xcoord=1.5+3*`x' if _n==`i'
}
tempfile stfe2050
save `stfe2050', replace

* append all together
use `stlin2020', clear
append using `stquad2020'
append using `stfe2020'
append using `stlin2050'
append using `stquad2050'
append using `stfe2050'
save data/figure/bin_estimates, replace

}

*** EOF
